#!/bin/ksh

# parse arguments
i=1
for PARAM in "$@"; do
	{ read IFACE IFMAX JUNK; IFNAMES[$i]="$IFACE"; IFMAXES[$i]="$IFMAX"; } <<- EOF
		$PARAM
	EOF
	(( i += 1 )) || true
done

# bytes
while read IFACE JUNK NETWORK JUNK IN OUT JUNK; do
	i=1
	for IFNAME in "${IFNAMES[@]}"; do
		if [[ "$IFACE" == "$IFNAME" && '<Link>' == "$NETWORK" ]]; then
			BYTES_IN[$i]="$IN"
			BYTES_OUT[$i]="$OUT"
		fi
		(( i += 1 )) || true
	done
done <<- EOF
	$(netstat -bvni)
EOF

# packets
while read IFACE JUNK NETWORK JUNK IN JUNK OUT JUNK; do
	i=1
	for IFNAME in "${IFNAMES[@]}"; do
		if [[ "$IFACE" == "$IFNAME" && '<Link>' == "$NETWORK" ]]; then
			PAK_IN[$i]="$IN"
			PAK_OUT[$i]="$OUT"
		fi
		(( i += 1 )) || true
	done
done <<- EOF
	$(netstat -vni)
EOF

# out
i=1
for IFNAME in "${IFNAMES[@]}"; do
	echo "iface,$IFNAME,${IFMAXES[$i]},${PAK_IN[$i]}:${PAK_OUT[$i]}:${BYTES_IN[$i]}:${BYTES_OUT[$i]}"
	(( i += 1 )) || true
done
